package com.yby6.framework.satoken.service;

import cn.dev33.satoken.stp.StpInterface;
import com.yby6.common.constant.HttpStatus;
import com.yby6.common.core.domain.ActiveUser;
import com.yby6.common.exception.YangBuYiException;
import com.yby6.common.satoken.LoginHelper;
import com.yby6.system.service.SysMenuService;
import com.yby6.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;


@RequiredArgsConstructor
@Component
public class StpInterfaceImpl implements StpInterface {

    @Lazy
    private final SysUserService sysUserService;

    @Lazy
    private final SysMenuService sysMenuService;


    /**
     * 返回账号拥有的权限码
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {
        ActiveUser activeUser = LoginHelper.getSysUser();
        List<String> permissions = activeUser.getPermissions();
        if (CollectionUtils.isEmpty(permissions) && !LoginHelper.isAdmin()) {
            throw new YangBuYiException(HttpStatus.UNAUTHORIZED, "您没有数据的权限，请联系管理员添加权限");
        } else if (LoginHelper.isAdmin()) {
            permissions.add("*:*");
        }
        return permissions;
    }

    /**
     * 返回账号拥有的角色ID
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {
        return LoginHelper.getSysUser().getRoles();
    }


}
